include Enum

class Player < Serializable

  attr_reader :source
  attr_reader :name

  def initialize (name)
    @name = name
    @source = []
  end

  def necessary_cards_count
    count = BALANCE_COUNT - @source.length
    if (count < 0)
      count = 0
    end
    return count
  end

  def receive(cards)
    @source.concat(cards)
  end

  def reset_highlights
    @source.each { |card| card.enabled = true }
  end

  def get_card(card)
    return source.delete(card)
  end

  def putAll(cards)
    receive(cards)
  end

  def get_card_with_min_value
    return get_card_with_lowest_value_in_the_set(@source)
  end

  def highlight_attacking_cards(tableCards)
    if (tableCards.empty?)
      @source.each { |card| card.enabled = true }
    else
      @source.each { |card| card.enabled = is_equal_rank?(tableCards, card) }
    end
  end

  def empty?
    return @source.empty?
  end

  def count
    return @source.length
  end

  protected

  def is_equal_rank? (tableCards, item)
    tableCards.each { |card|
      if (card.rank == item.rank)
        return true
      end
    }
    return false
  end

  def get_card_with_lowest_value_in_the_set(cards)
    return cards.min_by {|card| card.value }
  end

end